/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/dao/expression/Size.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: Size.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (周一, 02 七月 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.dao.expression;

import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;

/**
 * @author elancom
 * @date May 4, 2012
 */
public class Size implements Condition {
	private static final long serialVersionUID = -3298629384316516486L;

	private final String propertyName;

	private final int size;

	private final String op;

	/**
	 * @param propertyName
	 * @param size
	 * @param op
	 *            <ul>
	 *            <li>></li>
	 *            <li>=</li>
	 *            <li><</li>
	 *            <li>>=</li>
	 *            <li>!=</li>
	 *            <li><=</li>
	 *            </ul>
	 */
	public Size(String propertyName, int size, String op) {
		super();
		this.propertyName = propertyName;
		this.size = size;
		this.op = op;
	}

	@Override
	public Criterion toCriterion() {
		Criterion criterion = null;

		if ("=".equals(op)) {
			criterion = Restrictions.sizeEq(propertyName, size);
		} else if ("<=".equals(op)) {
			criterion = Restrictions.sizeLe(propertyName, size);
		} else if (">=".equals(op)) {
			criterion = Restrictions.sizeGe(propertyName, size);
		} else if ("<".equals(op)) {
			criterion = Restrictions.sizeLt(propertyName, size);
		} else if (">".equals(op)) {
			criterion = Restrictions.sizeGt(propertyName, size);
		} else if ("!=".equals(op)) {
			criterion = Restrictions.sizeNe(propertyName, size);
		}

		return criterion;
	}

	@Override
	public String toHQLString() {
		final StringBuilder sb = new StringBuilder();

		if ("=".equals(op)) {
			sb.append(propertyName).append(" = ").append(size);
		} else if ("<=".equals(op)) {
			sb.append(propertyName).append(" <= ").append(size);
		} else if (">=".equals(op)) {
			sb.append(propertyName).append(" >= ").append(size);
		} else if ("<".equals(op)) {
			sb.append(propertyName).append(" < ").append(size);
		} else if (">".equals(op)) {
			sb.append(propertyName).append(" > ").append(size);
		} else if ("!=".equals(op)) {
			sb.append(propertyName).append(" != ").append(size);
		}

		return sb.toString();
	}
}
